package HashMap源码;

/**
 * @Author: 朱胜伟
 * @CreateTime: 2021-01-29 16:28
 * @Description:
 */
public class ExtHashMap<K,V> implements ExtMap<K,V> {
    //1,定义table存放hashmap数据殼
    Node<K,V>[] table = null;  //默认没有初始化
    //2，实际用到table存储容量的大小
    int size;
    //3,负载因子0。75扩容才会用的到，负载因子越小，他的hash冲突越小
    float DEFAULT_LOAD_FACTOR = 0.75f;
    //4,table默认初始大小16
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
    @Override
    public V put(K k, V v) {
        //1,判断 table 是否为 null
        if(table == null){
            table = new Node[DEFAULT_INITIAL_CAPACITY];
        }
        //2,判断数组需不需要扩容
        //3，计算hash值，指定下标位置
        int index = getIndex(k,DEFAULT_INITIAL_CAPACITY);
        Node<K, V> kvNode = table[index];
        if(kvNode == null){
            //没有发生hash冲突
            table[index] = new Node<>(k,v,null);
        }else {

        }
        return null;
    }
    public int getIndex(K k,int length){
        int i = k.hashCode();
        int index = i % length;
        return index;
    }
    @Override
    public V get(K k) {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }
    //定义节点
    class Node<K,V> implements Entry<K,V>{
        private K key;
        private V value;
        //下一个节点
        private Node<K,V> next;

        public Node(K key, V value, Node<K, V> next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }

        @Override
        public K getKey() {
            return this.key;
        }

        @Override
        public V getValue() {
            return this.value;
        }

        @Override
        public V setValue(V value) {
            //设置新值的时候返回老值
            V ovl = this.value;
            this.value = value;
            return ovl;
        }
    }
}
